import {
	HTTP_REQUEST_URL,
	HEADER,
	TOKENNAME
} from '@/config/app';

// import {
// 	checkLogin
// } from '../libs/login';
import store from '@/store';


function toLogin() {
	console.log("========window.location.href", window.location.href)
	let arr = window.location.href.split('#')
	let path = '/#' + arr[1]
	console.log("======location", path)
	store.dispatch("getUserInfoAll", {
		path: path
	})

}

/**
 * 发送请求
 */
function baseRequest(url, method, data, {
	auth = false,
	noAuth = true,
	noVerify = false
}) {
	let Url = HTTP_REQUEST_URL,
		header = HEADER;
	//auth true 不需要登陆   否则需要登陆
	if (!auth) {
		if (window.location.href.indexOf('token') != '-1') {
			let tokes = window.location.href.split('token=')
			uni.setStorageSync('token', tokes[1]);
		}
		//默认登陆校验
		if (!uni.getStorageSync('token') && window.location.href.indexOf('token') == '-1') {
			toLogin();
		}
	}



	// if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;

	return new Promise((reslove, reject) => {
		let token = uni.getStorageSync('token')
		uni.request({
			url: Url + '/api/' + url,
			method: method || 'GET',
			header: {
				'token': token
			},
			data: data || {},
			success: (res) => {
				// #ifdef APP-PLUS
				console.log('app', Url + '/api/' + url, res.data);
				// #endif
				if (noVerify)
					reslove(res.data, res);
				else if (res.statusCode == 200)
					reslove(res.data, res);
				else if (res.statusCode == 401) {
					uni.setStorageSync('token','');
					// uni.removeStorageSync('token');
					// uni.removeStorageSync('userinfo');
					 toLogin();
					// reject(res.data);
				} else if (res.statusCode == 0) {
					// uni.showToast({
					// 	title: res.data.msg,
					// 	icon: "none",
					// 	duration: 3500
					// });
					// toLogin();
					// reject(res.data);
				} else if ([410000, 410001, 410002, 40000].indexOf(res.statusCode) !== -1) {
					reject(res.data);
				} else if (res.statusCode == 501) {
					reject(res.data);
				} else
					reject(res.data.message || '系统错误');
			},
			fail: (message) => {
				reject('请求失败');
			}
		})
	});
}

const request = {};

['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
	request[method] = (api, data, opt) => baseRequest(api, method, data, opt || {})
});

/**
 * 上传文件
 */
export function baseUploadRequest(url, data, {
	auth = false,
	noAuth = true,
	noVerify = false
}) {
	let Url = HTTP_REQUEST_URL,
		header = HEADER;
	//auth true 不需要登陆   否则需要登陆
	if (!auth) {
		if (window.location.href.indexOf('token') != '-1') {
			let tokes = window.location.href.split('token=')
			uni.setStorageSync('token', tokes[1]);
		}
		//默认登陆校验
		if (!uni.getStorageSync('token') && window.location.href.indexOf('token') == '-1') {
			toLogin();
		}
	}
	// if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;

	return new Promise((reslove, reject) => {
		let token = uni.getStorageSync('token')
		uni.uploadFile({
			url: Url + '/api/' + url,
			file: data.file,
			name: data.name,
			header: {
				'token': token
			},
			success: (res) => {
				// console.log(res)
				// #ifdef APP-PLUS
				console.log('app', Url + '/api/' + url, res.data);
				// #endif
				if (noVerify)
					reslove(res.data, res);
				else if (res.statusCode == 200){
					console.log("200",res)
					reslove(JSON.parse(res.data), res);
				}else if (res.statusCode == 401) {
					uni.setStorageSync('token','');
					toLogin();
					// reject(res.data);
				} else if (res.statusCode == 0) {
					// uni.showToast({
					// 	title: res.data.msg,
					// 	icon: "none",
					// 	duration: 3500
					// });
					// toLogin();
					// reject(res.data);
				} else if ([410000, 410001, 410002, 40000].indexOf(res.statusCode) !== -1) {
					reject(res.data);
				} else if (res.statusCode == 501) {
					reject(res.data);
				} else
					reject(res.data.message || '系统错误');
			},
			fail: (message) => {
				reject('请求失败');
			}
		})
	});
}



export default request;
